<?php

namespace App\Admin\Controllers;

use App\Models\CaseCaseProgress;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Dcat\Admin\Http\Controllers\AdminController;

class CaseCaseProgressController extends AdminController
{
    public function __construct(Request $request)
    {
        $this->case_id = $request->case_id;
        $this->trial_num = $request->trial_num;
        $this->trial_step = $request->trial_step;
        return $this;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CaseCaseProgress(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('case_case_id');
            $grid->column('trial_num');
            $grid->column('trial_step');
            $grid->column('content');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CaseCaseProgress(), function (Show $show) {
            $show->field('id');
            $show->field('case_case_id');
            $show->field('trial_num');
            $show->field('trial_step');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CaseCaseProgress(), function (Form $form) {
            $form->display('id');
            $form->hidden('case_case_id')->value($this->case_id);
            $form->hidden('trial_num')->value($this->trial_num);
            $form->hidden('trial_step')->value($this->trial_step);
            $form->hidden('content');
            if($this->trial_step == 1){  //判断是否是立案
                $form->text('step1_sudiao',"诉调号");
                $form->text('step1_fayuan',"立案法院");
                $form->date('step1_riqi',"立案日期");
                $form->currency('step1_jine',"诉请金额")->symbol('￥');
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step1_sudiao'] = $form->step1_sudiao;
                    $content['step1_fayuan'] = $form->step1_fayuan;
                    $content['step1_riqi'] = $form->step1_riqi;
                    $content['step1_jine'] = $form->step1_jine;
                    $form->content = json_encode($content);

                    $form->deleteInput('step1_sudiao');
                    $form->deleteInput('step1_fayuan');
                    $form->deleteInput('step1_riqi');
                    $form->deleteInput('step1_jine');

                });
            }

            if($this->trial_step == 2){  //判断是否是缴费阶段
                $form->date('step2_riqi',"缴费日期");
                $form->text('step2_anhao',"案号");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step2_riqi'] = $form->step2_riqi;
                    $content['step2_anhao'] = $form->step2_anhao;
                    $form->content = json_encode($content);
                    $form->deleteInput('step2_riqi');
                    $form->deleteInput('step2_anhao');

                });
            }



            if($this->trial_step == 3){  //判断是否是传票录入
                $form->text('step3_fangshi',"送达方式");
                $form->date('step3_riqi',"送达日期");
                $form->datetime('step3_kaiting',"开庭时间");
                $form->text('step3_faguan',"审理法官");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step3_fangshi'] = $form->step3_fangshi;
                    $content['step3_riqi'] = $form->step3_riqi;
                    $content['step3_kaiting'] = $form->step3_kaiting;
                    $content['step3_faguan'] = $form->step3_faguan;
                    $form->content = json_encode($content);
                    $form->deleteInput('step3_fangshi');
                    $form->deleteInput('step3_riqi');
                    $form->deleteInput('step3_kaiting');
                    $form->deleteInput('step3_faguan');

                });
            }


            if($this->trial_step == 4){  //判断是否是判决录入
                $form->date('step4_riqi',"判决日期");
                $form->currency('step4_jine',"判决金额")->symbol('￥');
                $form->text('step4_jieguo',"判决结果");
                // $form->text('step4_faguan',"执行法官");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step4_riqi'] = $form->step4_riqi;
                    $content['step4_jine'] = $form->step4_jine;
                    $content['step4_jieguo'] = $form->step4_jieguo;
                    // $content['step4_faguan'] = $form->step4_faguan;
                    $form->content = json_encode($content);
                    $form->deleteInput('step4_riqi');
                    $form->deleteInput('step4_jine');
                    $form->deleteInput('step4_jieguo');
                    // $form->deleteInput('step4_faguan');

                });
            }


            if($this->trial_step == 5){  //判断是否是调解
                $form->date('step5_tiaojie',"调解日期");
                $form->textarea('step5_jieguo',"调解结果");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step5_tiaojie'] = $form->step5_tiaojie;
                    $content['step5_jieguo'] = $form->step5_jieguo;
                    $form->content = json_encode($content);
                    $form->deleteInput('step5_tiaojie');
                    $form->deleteInput('step5_jieguo');

                });
            }

            if($this->trial_step == 6){  //判断是否申请判决
                $form->textarea('step6_beizhu',"备注");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step6_beizhu'] = $form->step6_beizhu;
                    $form->content = json_encode($content);
                    $form->deleteInput('step6_beizhu');

                });
            }


            if($this->trial_step == 7){  //判断是否申请判决
                $form->text('step7_anhao',"执行案号");
                $form->text('step7_faguan',"执行法官");
                $form->text('step7_lianxi',"联系方式");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step7_anhao'] = $form->step7_anhao;
                    $content['step7_faguan'] = $form->step7_faguan;
                    $content['step7_lianxi'] = $form->step7_lianxi;
                    $form->content = json_encode($content);
                    $form->deleteInput('step7_anhao');
                    $form->deleteInput('step7_faguan');
                    $form->deleteInput('step7_lianxi');

                });
            }

            if($this->trial_step == 8){  //判断是否撤案
                $form->text('step8_beizhu',"备注");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step8_beizhu'] = $form->step8_beizhu;
                    $form->content = json_encode($content);
                    $form->deleteInput('step8_beizhu');

                });
            }

            if($this->trial_step == 9){  //判断是否是结案
                $form->radio('step9_jieguo',"判决结果")->options([1 => '胜诉', 0=> '败诉'])->default(1);
                $form->textarea('step9_beizhu',"结果备注");
                $form->saving(function (Form $form) {
                    $content = array();
                    $content['step9_jieguo'] = $form->step9_jieguo;
                    $content['step9_beizhu'] = $form->step9_beizhu;
                    $form->content = json_encode($content);
                    $form->deleteInput('step9_jieguo');
                    $form->deleteInput('step9_beizhu');

                });
            }

            $form->display('created_at');
            $form->display('updated_at');

            $form->saved(function (Form $form) {
                # 将案件进度写入案件表
                $progress = CaseCaseProgress::find($form->getKey());
                $case = $progress->caseCase;
                unset($progress);
                $case->trial_num = $form->trial_num;
                $case->trial_step = $form->trial_step;
                $case->save();

                return $form->response()->success('保存成功')->redirect('casecases/' . $form->case_case_id);
            });
        });
    }
}
